package com.arethusa;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumn;

import simpleorm.core.SConnection;
import simpleorm.core.SPreparedStatement;
import simpleorm.core.SResultSet;

import com.arethusa.langebaandb.Vereniging;
import com.arethusa.langebaandb.VerenigingBean;
import com.ibm.j2x.swing.util.TableUtilities;
import com.ibm.j2x.util.CollectionUtilities;

public class ZoekVereniging extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel zoekPanel = null;
	private JScrollPane resultaatScrollPane = null;
	private JTable resultaatTable = null;
	private JLabel jLabel = null;
	private JTextField zoekTextField = null;
	private JButton zoekButton = null;
	private JButton okButton = null;
	private JButton cancelButton = null;
	private JPanel okCancelPanel = null;
	private List<VerenigingBean> olist;
	private JDialog parent = this;
	private VerenigingBean selectedDepotnr = null;

	private class TableSelectionListener implements ListSelectionListener {
	    public void valueChanged(ListSelectionEvent e) {
	        //Ignore extra messages.
	        if (e.getValueIsAdjusting()) return;

	        ListSelectionModel lsm = (ListSelectionModel)e.getSource();
	        if (lsm.isSelectionEmpty()) {
	            //no rows are selected
	        	getOkButton().setEnabled(false);
	        } else {
	            int selectedRow = lsm.getMinSelectionIndex();
	            //selectedRow is selected
	        	getOkButton().setEnabled(true);
	        	VerenigingBean r = (VerenigingBean)olist.get(selectedRow);
	        	selectedDepotnr = r;
	        }
	    }
	}
	
	/**
	 * This is the default constructor
	 */
	public ZoekVereniging() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(345, 237);
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.setModal(true);
		this.setName("searchDialog");
		this.setTitle("Zoeken naar Vereniging");
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			BorderLayout borderLayout = new BorderLayout();
			borderLayout.setHgap(0);
			borderLayout.setVgap(0);
			jContentPane = new JPanel();
			jContentPane.setLayout(borderLayout);
			jContentPane.add(getZoekPanel(), java.awt.BorderLayout.NORTH);
			jContentPane.add(getResultaatScrollPane(), java.awt.BorderLayout.CENTER);
			jContentPane.add(getOkCancelPanel(), java.awt.BorderLayout.SOUTH);
		}
		return jContentPane;
	}

	/**
	 * This method initializes zoekPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getZoekPanel() {
		if (zoekPanel == null) {
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.insets = new java.awt.Insets(5,5,5,5);
			gridBagConstraints2.gridy = 0;
			gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
			gridBagConstraints2.gridx = 2;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.ipadx = 331;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.insets = new java.awt.Insets(5,5,5,5);
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.insets = new java.awt.Insets(5,5,5,5);
			gridBagConstraints.gridy = 0;
			gridBagConstraints.gridx = 0;
			jLabel = new JLabel();
			jLabel.setText("Zoektekst");
			zoekPanel = new JPanel();
			zoekPanel.setLayout(new GridBagLayout());
			zoekPanel.add(jLabel, gridBagConstraints);
			zoekPanel.add(getZoekTextField(), gridBagConstraints1);
			zoekPanel.add(getZoekButton(), gridBagConstraints2);
		}
		return zoekPanel;
	}

	/**
	 * This method initializes resultaatScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getResultaatScrollPane() {
		if (resultaatScrollPane == null) {
			resultaatScrollPane = new JScrollPane();
			resultaatScrollPane.setViewportView(getResultaatTable());
		}
		return resultaatScrollPane;
	}

	/**
	 * This method initializes resultaatTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getResultaatTable() {
		if (resultaatTable == null) {
			resultaatTable = new JTable();
//			 Ask to be notified of selection changes.
			resultaatTable.getSelectionModel().addListSelectionListener(new TableSelectionListener());
		}
		return resultaatTable;
	}

	/**
	 * This method initializes zoekTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getZoekTextField() {
		if (zoekTextField == null) {
			zoekTextField = new JTextField();
		}
		return zoekTextField;
	}

	/**
	 * This method initializes zoekButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getZoekButton() {
		if (zoekButton == null) {
			zoekButton = new JButton();
			zoekButton.setText("Zoek");
			zoekButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					searchVereniging( getZoekTextField().getText() );
				}
			});
		}
		return zoekButton;
	}

	/**
	 * This method initializes okButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("Ok");
			okButton.setEnabled(false);
			okButton.setSelected(true);
			this.
			okButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					parent.setVisible(false);
					parent.dispose();
				}
			});
		}
		return okButton;
	}

	/**
	 * This method initializes canceljButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Sluiten");
			cancelButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					parent.setVisible(false);
					parent.dispose();
				}
			});
		}
		return cancelButton;
	}

	/**
	 * This method initializes okCanceljPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getOkCancelPanel() {
		if (okCancelPanel == null) {
			okCancelPanel = new JPanel();
			okCancelPanel.add(getOkButton(), null);
			okCancelPanel.add(getCancelButton(), null);
		}
		return okCancelPanel;
	}

	@SuppressWarnings("unchecked")
	private void searchVereniging(String searchString ) {
		List<VerenigingBean> tmp = new ArrayList<VerenigingBean>();
		boolean hasBegun = SConnection.getConnection().hasBegun();
		if( ! hasBegun ) { SConnection.begin(); }
		SPreparedStatement stmt = Vereniging.meta.select(
			"Naam SOUNDS LIKE '"+searchString+"' " +
			"OR UPPER(Naam) LIKE UPPER('%"+searchString+"%')",null);
		SResultSet res = stmt.execute();
		/// loop through the results
		while (res.hasNext()) {
			VerenigingBean strt = new VerenigingBean(((Vereniging)res.getRecord()));
			tmp.add(strt);
		}
		if( ! hasBegun ) { SConnection.commit(); }
    	olist = CollectionUtilities.observableList(tmp);
		TableUtilities.setViewToModel("LangebaanModel.xml", "Vereniging", getResultaatTable(), olist);
		TableColumn col1 = getResultaatTable().getColumnModel().getColumn(0);
		col1.setPreferredWidth(60);
		TableColumn col2 = getResultaatTable().getColumnModel().getColumn(1);
		col2.setPreferredWidth(200);
		return;
	}
	
	public String getSelectedDepotnr() {
		String str = "<Kies vereniging>";
		if( selectedDepotnr != null ) {
			int dnr = selectedDepotnr.getDepotnr().intValue();
			str=(dnr<10000?"0":"")+dnr+" "+selectedDepotnr.getNaam();
		}
		return str;
	}
	
	public void clear() {
		zoekTextField.setText("");
		searchVereniging("qxdfxvctyu");
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
